//
//  KeHuXiangQingGenJinJiLuTableViewCell.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/20.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "KeHuXiangQingGenJinJiLuTableViewCell.h"
#import "XieGenJinFuJianListTableViewCell.h"
#import "PhotoContainer.h"
@implementation KeHuXiangQingGenJinJiLuTableViewCell{
    PhotoContainer *_photosContainer;
    NSArray *dataSource;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _photosContainer = [[PhotoContainer alloc] initWithMaxItemsCount:10];
    _photosContainer.canBrowser = YES;
    [self.contentView addSubview:_photosContainer];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"XieGenJinFuJianListTableViewCell" bundle:nil] forCellReuseIdentifier:@"XieGenJinFuJianListTableViewCell"];
    

}

- (void)setModel:(AllModel *)model {
    _model = model;
    NSDictionary *dataDic = model.modelInfoDictionary;
    NSMutableArray *urlImageArray = [NSMutableArray array];
    if([[dataDic objectForKey:@"img"] isKindOfClass:[NSArray class]]){
        for (NSDictionary *temp in [dataDic objectForKey:@"img"]) {
            [urlImageArray addObject:GetHandleString(temp, @"urlImage")];
        }
    }
    
    UIView *bottomView = _contentLabel;
    
    if(urlImageArray.count > 0){
        _photosContainer.photoNamesArray = urlImageArray;
        _photosContainer.sd_layout
        .leftEqualToView(_contentLabel)
        .rightEqualToView(_contentLabel)
        .topSpaceToView(_contentLabel, 12);
        _photosContainer.hidden = NO;
        bottomView = _photosContainer;
    }else{
        _photosContainer.sd_layout
        .leftEqualToView(_contentLabel)
        .rightEqualToView(_contentLabel)
        .topSpaceToView(_contentLabel, 0);
        _photosContainer.hidden = YES;
    }
    if([[dataDic objectForKey:@"file"] isKindOfClass:[NSArray class]]){
        dataSource = [dataDic objectForKey:@"file"];
    }else{
        dataSource = [NSArray array];
    }
    [_mainTableView reloadData];
    
    _tableViewHeight.constant = 52*dataSource.count;
    
    
    [_headImage sd_setImageWithURL:WebImagePath(GetHandleString(dataDic, @"urlImage")) placeholderImage:HeadImagePlaceholder];
    _nameLabel.text = GetHandleString(dataDic, @"realname");
    _buMenLabel.text = GetHandleString(dataDic, @"deptName");
    _genJinTimeLabel.text = GetHandleString(dataDic, @"nextTime");
    _chuangjianShiJianTime.text = GetHandleString(dataDic, @"createTime");
    _contentLabel.text = GetHandleString(dataDic, @"content");
    _genJinFangShiLabel.text = GetHandleString(dataDic, @"category");
    
    
    
    if([GetHandleString(dataDic, @"nextTime") length] == 0){
        _genJinShiJianView.hidden = YES;
        _genJinShiJianHeight.constant = 0;
        _genJinShiJianTopSapce.constant = 0;
        
        if([GetHandleString(dataDic, @"category") length] == 0){
            _genJinFangShiView.hidden = YES;
            _genJinFangShiTopSpace.constant = 0;
            _genJinFangShiViewHeight.constant = 0;
            [self setupAutoHeightWithBottomView:bottomView bottomMargin:79+10+52*dataSource.count-38];
        }else{
            _genJinFangShiView.hidden = NO;
            _genJinFangShiTopSpace.constant = 12;
            _genJinFangShiViewHeight.constant = 30;
            [self setupAutoHeightWithBottomView:bottomView bottomMargin:79+10+52*dataSource.count-38+42];
        }
        
        
    }else{
        _genJinShiJianView.hidden = NO;
        _genJinShiJianHeight.constant = 32;
        _genJinShiJianTopSapce.constant = 6;
        
        
        if([GetHandleString(dataDic, @"category") length] == 0){
            _genJinFangShiView.hidden = YES;
            _genJinFangShiTopSpace.constant = 0;
            _genJinFangShiViewHeight.constant = 0;
            [self setupAutoHeightWithBottomView:bottomView bottomMargin:79+10+52*dataSource.count];
        }else{
            _genJinFangShiView.hidden = NO;
            _genJinFangShiTopSpace.constant = 12;
            _genJinFangShiViewHeight.constant = 30;
            [self setupAutoHeightWithBottomView:bottomView bottomMargin:79+10+52*dataSource.count+42];
        }
        
        
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XieGenJinFuJianListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XieGenJinFuJianListTableViewCell"];
    NSDictionary *dataDic = dataSource[indexPath.row];
    cell.nameLabel.text = GetHandleString(dataDic, @"name");
    cell.sizeLabel.text = [self fileSizeConversion:[GetHandleString(dataDic, @"size") integerValue]];
    cell.manageClickBlock = ^(NSInteger index) {
        if(_manageClickBlock){
            _manageClickBlock(dataDic,index);
        }
    };
    return cell;
}

//将文件大小转换为 M/KB/B
- (NSString *)fileSizeConversion:(NSInteger)totalSize {
    NSString *totleStr = nil;
    if (totalSize > 1024 * 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fM",totalSize / 1024.00f /1024.00f];
    } else if (totalSize > 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fKB",totalSize / 1024.00f ];
        
    } else {
        totleStr = [NSString stringWithFormat:@"%.2fB",totalSize / 1.00f];
    }
    return totleStr;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52;
}
- (IBAction)bianJiClick:(UIButton *)sender {
    if(_manageClickBlock){
        _manageClickBlock(_model.modelInfoDictionary,103);
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
